package javagprssrv.Common;

import java.io.Writer;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.util.Vector;
import java.io.Reader;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.File;

public class INI
{
    public java.lang.String INIFileName;
    
    public INI(final java.lang.String _INIFileName) {
        this.INIFileName = "";
        this.INIFileName = _INIFileName;
    }
    
    public java.lang.String INIRead(java.lang.String GroupName, java.lang.String ValueName) {
        java.lang.String ret = null;
        final java.io.File f = new java.io.File(this.INIFileName);
        //System.err.print(f.getAbsolutePath());
        if (!f.exists()) {
            return ret;
        }
        GroupName = GroupName.trim();
        ValueName = ValueName.trim();
        try {
            final java.io.FileReader fr = new java.io.FileReader(this.INIFileName);
            
            //java.lang.System.out.println(fr.toString());
            final java.io.BufferedReader br = new java.io.BufferedReader(fr);
            java.lang.String iniLine = "";
            boolean isInGroup = false;
            while (iniLine != null) {
            	String s = br.readLine();            	
                if(s==null){
                    break;
                }
                System.out.println(s);
                iniLine = s.trim();
                if (iniLine.trim().length() == 0) {
                    continue;
                }
                if (iniLine.indexOf("//") > -1) {
                    iniLine = iniLine.substring(0, iniLine.indexOf("//"));
                    if (iniLine.trim().length() == 0) {
                        continue;
                    }
                }
                if (!isInGroup) {
                    if (!iniLine.equals("[" + GroupName + "]")) {
                        continue;
                    }
                    isInGroup = true;
                    if (ValueName.length() == 0) {
                        return GroupName;
                    }
                    continue;
                }
                else {
                    if (iniLine.length() > 0 && iniLine.substring(0, 1).equals("[") && iniLine.substring(iniLine.length(), iniLine.length() + 1).equals("]")) {
                        return ret;
                    }
                    final int offsetDY = iniLine.indexOf("=");
                    if (offsetDY == -1) {
                        continue;
                    }
                    final java.lang.String VN = iniLine.substring(0, offsetDY);
                    final java.lang.String VV = iniLine.substring(offsetDY + 1, iniLine.length());
                    if (VN.equals(ValueName)) {
                        ret = VV.trim();
                        return ret;
                    }
                    continue;
                }
            }
            br.close();
            fr.close();
        }
        catch (java.lang.Exception e) {
            e.printStackTrace();
            java.lang.System.out.println("INI().INIRead(" + GroupName + "," + ValueName + ")出错！");
            java.lang.System.out.println(e.toString());
            java.lang.System.out.println(e.getMessage());
        }
        return ret;
    }
    
    public boolean INIWrite(java.lang.String GroupName, java.lang.String ValueName, final java.lang.String ValueValue) {
        final boolean ret = false;
        final java.io.File f = new java.io.File(this.INIFileName);
        final java.util.Vector<java.lang.String> INIs = new java.util.Vector<java.lang.String>();
        if (!f.exists()) {
            try {
                f.createNewFile();
            }
            catch (java.lang.Exception ex) {}
        }
        GroupName = GroupName.trim();
        ValueName = ValueName.trim();
        try {
            final java.io.FileReader fr = new java.io.FileReader(this.INIFileName);
            final java.io.BufferedReader br = new java.io.BufferedReader(fr);
            java.lang.String iniLine = "";
            while (iniLine != null) {
                iniLine = br.readLine().trim();
                INIs.addElement(iniLine);
            }
            br.close();
            fr.close();
        }
        catch (java.lang.Exception ex2) {}
        int GroupStartIndex = -1;
        boolean isFindValue = false;
        boolean isFindGroup = false;
        boolean isFindValueInGroup = false;
        for (int i = 0; i <= INIs.size() - 1; ++i) {
            java.lang.String s1 = INIs.elementAt(i).toString().trim();
            s1 += "                                                                                ";
            if (isFindGroup && s1.substring(0, 1).equals("[") && !isFindValue) {
                isFindValueInGroup = false;
            }
            if (s1.trim().equals("[" + GroupName + "]")) {
                GroupStartIndex = i + 1;
                isFindGroup = true;
            }
            if (s1.substring(0, ValueName.length() + 1).equals(ValueName + "=")) {
                INIs.setElementAt(ValueName + "=" + ValueValue, i);
                isFindValue = true;
                isFindValueInGroup = true;
            }
        }
        if (isFindGroup) {
            if (!isFindValue) {
                final java.lang.String s2 = ValueName + "=" + ValueValue;
                INIs.insertElementAt(s2, GroupStartIndex);
            }
        }
        else {
            INIs.addElement("[" + GroupName + "]");
            INIs.addElement(ValueName + "=" + ValueValue);
        }
        try {
            final java.io.BufferedWriter output = new java.io.BufferedWriter(new java.io.FileWriter(f));
            for (int j = 0; j <= INIs.size() - 1; ++j) {
                final java.lang.String s3 = INIs.elementAt(j).toString().trim();
                output.write(s3);
                output.newLine();
            }
            output.close();
        }
        catch (java.lang.Exception ex3) {}
        return ret;
    }
}
